package com.jd.pet.service.impl;

import com.jd.basic.service.IBaiduAiAuditService;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.LoginContext;
import com.jd.org.domain.Employee;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.pet.domain.Pet;
import com.jd.pet.domain.PetDetail;
import com.jd.pet.domain.PetOnlineAuditLog;
import com.jd.pet.mapper.PetDetailMapper;
import com.jd.pet.mapper.PetMapper;
import com.jd.pet.mapper.PetOnlineAuditLogMapper;
import com.jd.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    @Transactional
    public void save(Pet pet) {
        //添加宠物 - 自增id
        super.save(pet);

        //添加宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }


    @Override
    @Transactional
    public void remove(Long id) {
        //删除宠物信息
        super.remove(id);
        //删除宠物详情
        petDetailMapper.removeByPetId(id);
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        //修改宠物信息
        super.update(pet);

        //修改宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.update(petDetail);
        }
    }

    @Override
    @Transactional
    public void patchRemove(List<Long> ids) {
        //删除宠物信息
        super.patchRemove(ids);
        //删除宠物详情
        petDetailMapper.patchRemoveByPetId(ids);
    }


    // 上架
    @Override
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        // 1.对每一个宠物审核，name和resource
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            // 审核文本
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            // 审核图片
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());

            // 公共信息
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(id);
            //获取登陆信息id
            Long logininfoId = LoginContext.getLogininfo(request).getId();
            Employee employee = employeeMapper.loadByLogininfoId(logininfoId);
            log.setAuditId(employee.getId());


            if (boo1 && boo2){
                petMapper.onsale(new Date(),id);

                log.setState(1);// 状态1，上架
                //审核日志
                log.setNote("审核成功");
                petOnlineAuditLogMapper.save(log);

            }else {
                //3.审核失败：记录审核日志
                log.setState(0);//0表示审核失败
                log.setNote("审核失败，图片和文本非法!!!");//审核日志
                petOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("审核失败：图片，文本非法，或图片不存在");
            }

        }

        return new AjaxResult();
    }


    // 下架
    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        // 操作数据库：改下架时间=当前系统时间，0为下架
        petMapper.offsale(new Date(),ids);
    }
}
